package com.wanshi.servlet;

import com.wanshi.entity.Product;
import com.wanshi.service.ProdService;
import com.wanshi.utils.Constants;
import com.wanshi.utils.PicUtils;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@SuppressWarnings("all")
@WebServlet("/seller/updateProd")
public class updateProd extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {


        Map<String, Object> map = new HashMap<String, Object>();

        ServletContext context = this.getServletContext();
        String picUrl = (String) context.getAttribute("pic_save_path");


        // 1 搞一个工厂
        DiskFileItemFactory factory = new DiskFileItemFactory();
        // 2 搞一个ServletFileUpload对象
        ServletFileUpload upload = new ServletFileUpload(factory);
        upload.setFileSizeMax(1024 * 1024 * 5);//


        // 3 解析request
        try {
            List<FileItem> items = upload.parseRequest(request);


            // 4 一种是普通项 文件上传项
            for (FileItem item : items) {
                if (item.isFormField()) {
                    // 普通字段
                    String name = item.getFieldName();
                    String value = item.getString("utf-8");

                    map.put(name, value);
                } else {
                    // 文件上传域
                    // 一个是字节输入流 输出流
                    String name = item.getName();
                    if (name == null || name.trim().equals("")) {
                        response.getWriter().write("请选择图片");

                    }else{
                        int lastIndex = name.lastIndexOf(".");
                        name = name.substring(lastIndex);

                        String uuidname = UUID.randomUUID().toString().replace("-","") + name;
                        FileOutputStream fos = new FileOutputStream(picUrl+ uuidname);
                        InputStream is = item.getInputStream();

                        byte[] bs = new byte[1024];
                        int len = 0;
                        while ((len = is.read(bs)) != -1) {
                            fos.write(bs, 0, len);
                        }

                        fos.close();
                        is.close();

                        //aa.jpg    aa_s.jpg
                        PicUtils picUtils = new PicUtils(picUrl+uuidname);
                        picUtils.resizeByWidth(200);
                        map.put("imgurl", uuidname);
                    }

                }
            }

            // 文件上传完成,保存相关信息到数据库
            Product prod = new Product();
            try {
                BeanUtils.populate(prod, map);

                // 添加用户
                ProdService prodService = new ProdService();
                prodService.updateProd(prod);


                response.sendRedirect(request.getContextPath()+"/seller/findProdByUser");
                //response.getWriter().write("恭喜您,添加成功,3秒后跳转到首页");
                //response.setHeader("Refresh", "3;url=" + request.getContextPath() + "/index.jsp");
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
        } catch (FileUploadException e) {
            e.printStackTrace();
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
